<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单编号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="添加时间" prop="addtime">
        <el-date-picker clearable
                        v-model="queryParams.addtime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择添加时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="订单总价" prop="total">
        <el-input
          v-model="queryParams.total"
          placeholder="请输入订单总价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <!--        <el-input-->
        <!--          v-model="queryParams.status"-->
        <!--          placeholder="请输入订单状态"-->
        <!--          clearable-->
        <!--          @keyup.enter.native="handleQuery"-->
        <!--        />-->
        <el-select
          v-model="queryParams.status"
          clearable
          placeholder="请输入订单状态">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          :disabled="multiple"
          @click="handleAdd"
          v-hasPermi="['backstage:orderinfo:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['backstage:orderinfo:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['backstage:orderinfo:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['backstage:orderinfo:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderinfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="订单id" align="center" prop="id"/>
      <el-table-column label="用户id" align="center" prop="userId"/>
      <el-table-column label="订单编号" align="center" prop="code"/>
      <el-table-column label="添加时间" align="center" prop="addtime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.addtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单总价" align="center" prop="total"/>
      <el-table-column label="收货地址" align="center" prop="address">
        <template slot-scope="scope">
          <el-tooltip placement="top">
            <div slot="content">
              收货人：{{ scope.row.address.name }}<br/>
              电&emsp;话：{{ scope.row.address.phone }}<br>
              省市区：{{ scope.row.address.province }}-{{ scope.row.address.city }}-{{ scope.row.address.area }}<br>
              地&emsp;址：{{ scope.row.address.address }}
            </div>
            <el-button>查看地址</el-button>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 2">已完成</el-tag>
          <el-tag type="warning" v-if="scope.row.status == 1">已支付</el-tag>
          <el-tag type="danger" v-if="scope.row.status == 0">待支付</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="订单信息" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" plain size="small" @click="orderItem(scope.row.id)">订单细节</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="oTitle" :visible="oOpen" width="800px" :before-close="oderItemClose">
      <el-table :data="oItem">
        <el-table-column label="订单细节id" align="center" prop="id"/>
        <el-table-column label="商品id" align="center" prop="goodsId"/>
        <el-table-column label="商品名称" align="center" prop="goods.name">
        </el-table-column>
        <el-table-column label="数量" align="center" prop="num"/>
        <el-table-column label="总价" align="center" prop="total"/>
        <el-table-column label="细节状态" align="center" prop="status">
          <template slot-scope="scope">
            <el-tag type="warning" v-if="scope.row.status == 0">未发货</el-tag>
            <el-tag type="success" v-if="scope.row.status == 1">已发货</el-tag>
            <el-tag v-if="scope.row.status == 2">已收货</el-tag>
            <el-tag type="info" v-if="scope.row.status == 3">已评价</el-tag>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import {
  listOrderinfo,
  getOrderinfo,
  delOrderinfo,
  addOrderinfo,
  updateOrderinfo,
  getOrderDetails
} from "@/api/backstage/orderinfo";

export default {
  name: "Orderinfo",
  data() {
    return {
      options: [{
        value: '0',
        label: '未支付'
      }, {
        value: '1',
        label: '已支付'
      }, {
        value: '2',
        label: '交易完成'
      }],

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单信息表格数据
      orderinfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        code: null,
        addtime: null,
        total: null,
        addressId: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          {required: true, message: "用户id不能为空", trigger: "blur"}
        ],
        code: [
          {required: true, message: "订单编号不能为空", trigger: "blur"}
        ],
        addtime: [
          {required: true, message: "添加时间不能为空", trigger: "blur"}
        ],
        isdelete: [
          {required: true, message: "是否删除   0-未删除    1-已删除不能为空", trigger: "blur"}
        ],
        status: [
          {required: true, message: "订单状态   0-未支付  1-已支付  2-已收货不能为空", trigger: "blur"}
        ]
      },

      //订单细节
      oTitle: '',
      oOpen: false,
      oItem: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单信息列表 */
    getList() {
      this.loading = true;
      listOrderinfo(this.queryParams).then(response => {
        this.orderinfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      // this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        code: null,
        addtime: null,
        total: null,
        addressId: null,
        isdelete: null,
        status: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      // this.open = true;
      // this.title = "添加订单信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrderinfo(id).then(response => {
        this.form = response.data;
        // this.open = true;
        this.title = "修改订单信息";
      });
    },
    /** 提交按钮
     submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrderinfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrderinfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },*/
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单信息编号为"' + ids + '"的数据项？').then(function () {
        return delOrderinfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('backstage/orderinfo/export', {
        ...this.queryParams
      }, `orderinfo_${new Date().getTime()}.xlsx`)
    },
    /** 订单细节 */
    orderItem(id) {
      getOrderDetails(id).then(res => {
        // console.log(res);
        this.oItem = res.data;
        this.oTitle = "订单细节";
        this.oOpen = true;
      })

    },
    oderItemClose(){
      this.oOpen = false;
    }
  }
};
</script>
